
const mongoose = require('mongoose')
const { Schema } = mongoose

// 模型
const AdminUserSchema = new Schema({
    username: { type: String },
    password: { 
        type: String,
        select: false,  // 查询时，不查询密码
        set(psw) {
            return require('bcrypt').hashSync(psw, 10)  // 使用 bcrypt 对密码进行散列加密，加密等级是 10
        }
    },
})

// 解决报错: Cannot overwrite `AdminUser` model once compiled. 
const AdminUser = mongoose.models.AdminUser || mongoose.model('AdminUser', AdminUserSchema)

module.exports = AdminUser